package com.tengfei.api.question;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.AddGroup;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.common.excel.ExcelResult;
import com.tengfei.common.utils.poi.ExcelUtil;
import com.tengfei.main.question.domain.bo.ObjAndSubBo;
import com.tengfei.main.question.domain.bo.ObjectiveBo;
import com.tengfei.main.question.domain.bo.SubjectiveBo;
import com.tengfei.main.question.domain.export.ObjExportVo;
import com.tengfei.main.question.domain.export.ObjImportVo;
import com.tengfei.main.question.domain.export.SubExportVo;
import com.tengfei.main.question.domain.export.SubImportVo;
import com.tengfei.main.question.service.IObjAndSubService;
import com.tengfei.main.question.service.excel.ObjImportListener;
import com.tengfei.main.question.service.excel.SubImportListener;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 客观题库Controller
 *
 * @author tengfei
 * @date 2022-07-28
 */
@Validated
@Api(value = "题目控制器", tags = {"题目列表"})
@RestController
@RequestMapping("/question/question")
public class ObjAndSubController extends BaseController {

    @Resource
    private IObjAndSubService objAndSubService;

    /**
     * 查询题目列表
     */
    @ApiOperation("查询题目列表")
    @SaCheckPermission("question:question:list")
    @GetMapping("/list")
    public TableDataInfo list(ObjAndSubBo bo, PageQuery pageQuery) {
        return objAndSubService.queryQuPageList(bo, pageQuery);
    }

    /**
     * 导出题目列表
     */
    @ApiOperation("导出题目列表")
    @SaCheckPermission("question:question:export")
    @Log(title = "导出题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ObjAndSubBo bo, HttpServletResponse response) {
        List list = objAndSubService.queryList(bo);
        if (objAndSubService.isObj(bo.getQuType())) {
            List<ObjExportVo> objExportVos = BeanUtil.copyToList(list, ObjExportVo.class);
            ExcelUtil.exportExcel(objExportVos, "客观题库列表", ObjExportVo.class, response);
        } else {
            List<SubExportVo> subExportVos = BeanUtil.copyToList(list, SubExportVo.class);
            ExcelUtil.exportExcel(subExportVos, "主观题库列表", SubExportVo.class, response);
        }
    }

    /**
     * 获取题目详细信息
     */
    @ApiOperation("获取题目详细信息")
    @SaCheckPermission("question:question:query")
    @GetMapping("/")
    public R<Object> getInfo(@ApiParam("题目id") @RequestParam("quId") Long quId, @ApiParam("题目类型") @RequestParam("quType") Integer quType) {
        return R.ok(objAndSubService.queryByIdAndType(quId, quType));
    }

    /**
     * 新增客观题目
     */
    @ApiOperation("新增客观题目")
    @SaCheckPermission("question:question:add")
    @Log(title = "新增客观题目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addObj")
    public R<Void> addObj(@Validated(AddGroup.class) @RequestBody ObjectiveBo bo) {
        return toAjax(objAndSubService.insertObjByBo(bo) ? 1 : 0);
    }

    /**
     * 新增主观题目
     */
    @ApiOperation("新增主观题目")
    @SaCheckPermission("question:question:add")
    @Log(title = "新增主观题目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addSub")
    public R<Void> addSub(@Validated(AddGroup.class) @RequestBody SubjectiveBo bo) {
        return toAjax(objAndSubService.insertSubByBo(bo) ? 1 : 0);
    }

    /**
     * 修改客观题目
     */
    @ApiOperation("修改客观题目")
    @SaCheckPermission("question:question:edit")
    @Log(title = "修改客观题目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editObj")
    public R<Void> editObj(@Validated(EditGroup.class) @RequestBody ObjectiveBo bo) {
        return toAjax(objAndSubService.updateObjByBo(bo) ? 1 : 0);
    }

    /**
     * 修改主观题目
     */
    @ApiOperation("修改主观题目")
    @SaCheckPermission("question:question:edit")
    @Log(title = "修改主观题目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editSub")
    public R<Void> editSub(@Validated(EditGroup.class) @RequestBody SubjectiveBo bo) {
        return toAjax(objAndSubService.updateSubByBo(bo) ? 1 : 0);
    }

    /**
     * 批量删除题目
     */
    @ApiOperation("批量删除题目")
    @SaCheckPermission("question:question:remove")
    @Log(title = "批量删除题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("题目id与类型") @RequestBody() List<ObjAndSubBo> bos) {
        return toAjax(objAndSubService.deleteByIds(bos) ? 1 : 0);
    }

    /*
     *  导入客观题
     */
    @ApiOperation("导入客观题列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "导入文件", dataType = "java.io.File", required = true),
    })
    @Log(title = "导入客观题列表", businessType = BusinessType.IMPORT)
    @SaCheckPermission("question:question:import")
    @PostMapping("/importObj")
    public R<Void> importObjData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<ObjImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ObjImportVo.class, new ObjImportListener());
        return R.ok(result.getAnalysis());
    }

    /*
     *  导入主观题
     */
    @ApiOperation("导入主观题列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "导入文件", dataType = "java.io.File", required = true),
    })
    @Log(title = "导入主观题列表", businessType = BusinessType.IMPORT)
    @SaCheckPermission("question:question:import")
    @PostMapping("/importSub")
    public R<Void> importSubData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<SubImportVo> result = ExcelUtil.importExcel(file.getInputStream(), SubImportVo.class, new SubImportListener());
        return R.ok(result.getAnalysis());
    }

    /*
     *  导入主观题
     */
    @ApiOperation("下载题目导入模板(1:客观题模板,2:主观题模板)")
    @PostMapping("/quTemplate/{type}")
    public void importTemplate(HttpServletResponse response, @PathVariable("type") Integer type) {
        if (ObjectUtil.isNotNull(type)) {
            if (type == 1) {
                ExcelUtil.exportExcel(new ArrayList<>(), "客观题数据", ObjImportVo.class, response);
            } else {
                ExcelUtil.exportExcel(new ArrayList<>(), "主观题数据", SubImportVo.class, response);
            }
        }
    }


}
